"""
演示捕获异常
"""

# 基本捕获语法
# try:
#     f=open("D:/桌面/李/Icons/word.txt","r",encoding="UTF-8")
# except:
#     print("出现异常了，因为文件不存在，我将open的模式，改为w模式去打开")
#     f=open("D:/桌面/李/Icons/word.txt","w",encoding="UTF-8")

# 捕获指定的异常
# try:
#     print(name)
#     # 1/0
# except NameError as e:
#     print("出现了变量未定义的异常")
#     print(e)

#捕获多个异常
# try:
#     1/0
#     print(name)
# except(NameError,ZeroDivisionError) as e:
#     print("出现了变量未定义或除以0的异常错误")

#捕获所有异常
try:
    f=open("D:/桌面/李/Icons/word2.txt","r")
except Exception as e:
    print("出现异常了")
    f=open("D:/桌面/李/Icons/word2.txt","w",encoding="UTF-8")
else:
    print("无异常")
finally:
    print("finally,有无异常都执行")
    f.close()
